<?php
namespace Admin\Model;

use Think\Model;

class UserModel extends Model
{
    protected $_validate = array(
        array('LoginName', 'require', '用户名必填！'), //默认情况下用正则进行验证
        array('LoginName','/^[a-z0-9\x{4e00}-\x{9fa5}]{2,50}$/u','用户名必须50个字母以下',0,'regex',3),
        // 在新增的时候验证字段是否唯一
        array('LoginName','','用户名已经存在！',0,'unique',1),
        // 在修改时候验证字段是否唯一,需要配合主键
        array('LoginName,ID','','用户名已经存在！',1,'unique',2),

    );
    protected $_auto = array(
        array('SystemID', '0'),  // 新增的时候把 SytemID 字段设置为0
        array('Status', '1'),  // 新增的时候把 Status 字段设置为1
    );

    /**
     * 列表
     * @return mixed 列表
     */
    public function pageList()
    {
        //创建数据模型
        $classes = D('User');
        //排序
        $order = I('order');
        $order_column = $order[0]['column'];    //排序字段从0开始
        $order_dir = $order[0]['dir'];          //asc,desc 升序和降序

        $orderColumn = '';
        if (isset($order_column)) {
            $i = intval($order_column);
            switch ($i) {
                case 0:
                    $orderColumn = 'LoginName ' . $order_dir;
                    break;
                case 1:
                    $orderColumn = 'UserType ' . $order_dir;
                    break;
                case 2:
                    $orderColumn = 'Status ' . $order_dir;
                    break;
                case 3:
                    $orderColumn = 'RegistionTIme ' . $order_dir;
                    break;
                case 4:
                    $orderColumn = 'LoginCount ' . $order_dir;
                    break;
                case 5:
                    $orderColumn = 'LastLoginTime ' . $order_dir;
                    break;
            }
        }
        //搜索
        //$search = I('search')['value'];
        $column = I('columns');
        //分页
        $start = I('start');    //从多少开始
        $length = I('length');  //数据长度
        //表的总记录数
        $recordsTotal = $classes->count();
        //条件筛选后记录数
        $recordsFiltered = $recordsTotal;

        //设定查询条件
        $where = '1=1';
        $hasWhere = false;
        if (strlen($column[0]["search"]["value"]) > 0) {
            $hasWhere = true;
            $where = $where . ' AND LoginName LIKE \'%' . $column[0]["search"]["value"] . '%\'';
        }
        if (strlen($column[1]["search"]["value"]) > 0) {
            $hasWhere = true;
            $where = $where . ' AND UserType = ' . $column[1]["search"]["value"];
        }


        if ($hasWhere) {
            $recordsFiltered = $classes->where($where)->count();
        }

        //获取列表
        $list = $classes->where($where)->limit($start, $length)->order($orderColumn)->select();
        if($list == null)
        {
            $list=array();
        }
        //格式化数据
        foreach ($list as $k => $v) {
            $list[$k]['StatusDesc'] = C('GC.StstuText')[$v['status']];
            $list[$k]['RegistionTime'] = datetime($v['registiontime']);
            $list[$k]['LastLoginTime'] = datetime($v['lastlogintime']);
        }
        //创建返回模型
        $data['data'] = $list;
        $data['recordsTotal'] = $recordsTotal;
        $data['recordsFiltered'] = $recordsFiltered;
        //返回数据
        return $data;
    }

    /*
     * 登录
     */
    public function Login($userName,$password)
    {
        $user = M('User');

        $map['LoginName']=array('eq',$userName);
        $map['UserType']=array('neq',4);
        $map['Status']=array('eq',1);
        $cnt = $user->where($map)->Count();

        $result = array('Success' => 0,
            'UserID' => 0,
            'NickName' => '',
            'ErrorMsg' =>  '');

        if($cnt < 1)
        {
            //指定账户不存在
            $result['ErrorMsg'] = '指定账户不存在';
        }else{
            //匹配密码
            $data = $user->where($map)->find();

            if($data['pwd'] != $password)
            {
                //密码错误
                $result['ErrorMsg'] = '密码错误';
            }
            else
            {
                //更新登录
                $user->LastLoginTime = $data['currentlogintime'];
                $user->CurrentLoginTime = datetime();
                $user->LoginCount = $data['logincount']+1;
                $updateResult = $user->where('ID=\''.$data['id'].'\'')->save();

                $result['NickName'] = $data['realname'];
                $result['UserID'] = $data['id'];
                $result['Success'] = 1;
            }
        }

        return $result;
    }
}